/**
 * Project Name: mms-provider-user
 * File Name: DepartmentServiceImpl
 * Package Name: com.scb.dev.mms.service.impl
 * Date: 2018/11/30 08:40 PM
 * Copyright (c) 2018, Rainson.Yang All Rights Reserved.
 */
package com.scb.dev.mms.service.impl;

import com.scb.dev.mms.common.DataCommon;
import com.scb.dev.mms.common.ResponseResult;
import com.scb.dev.mms.common.ResultType;
import com.scb.dev.mms.common.StatusCode;
import com.scb.dev.mms.dao.AccountMapper;
import com.scb.dev.mms.dao.DepartmentMapper;
import com.scb.dev.mms.pojo.Department;
import com.scb.dev.mms.service.IDepartmentService;
import com.scb.dev.mms.util.DateTool;
import com.scb.dev.mms.util.UUIDTool;
import com.scb.dev.mms.vo.DepartmentVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: DepartmentServiceImpl
 * Description: DepartmentService 业务实现类
 * Date: 2018/11/30 08:40 PM
 *
 * @author Rainson.Yang
 * @version V1.0
 * @since JDK 1.8
 */
@Slf4j
@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private AccountMapper accountMapper;

    /**
     * insertDepartment
     * @MethodName: insertDepartment
     * @Description: add a new department
     *
     * @Param: Department department
     * @Return: ResponseResult success:{true, "add success", 202}
     * @Date: 2018/12/2 19:03
     */

    @Override
    public ResponseResult insertDepartment(Department department) {
        if (null == department) {
            log.info("department is null,fail");
            return new ResponseResult(false, ResultType.PARAMETER_NULL, StatusCode.PARAMETERNULL);
        }
        department.setDepartmentId(UUIDTool.generateUUID());
        if (departmentMapper.insertSelective(department) == 0) {
            log.info("insert fail");
            return new ResponseResult(false, ResultType.ADD_ERROR, StatusCode.ADDERROR);
        }
        return new ResponseResult(true, ResultType.ADD_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    /**
     * deleteDepartment: delete department by id
     *
     * @param departmentId
     * @return  success: {true, "delete success", 203}
     */
    @Override
    public ResponseResult deleteDepartment(String departmentId) {
        if (StringUtils.isBlank(departmentId)) {
            log.info("departmentId is null ,delete fail");
            return new ResponseResult(false, ResultType.PARAMETER_NULL, StatusCode.PARAMETERNULL);
        }
        int result = departmentMapper.deleteByDepartmentId(departmentId);
        if (result == 0) {
            log.info("delete fail");
            return new ResponseResult(false, ResultType.DELETE_ERROR, StatusCode.DELETEERROR);
        }
        log.info("deleted " + result + " items");
        return new ResponseResult(result, true, ResultType.DELETE_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    /**
     * updateDepartment: update department by id
     *
     * @param department
     * @return  success: {true, "update success", 204}
     */
    @Override
    public ResponseResult updateDepartment(Department department) {
        if (null == department) {
            log.info("department is null ,fail");
            return new ResponseResult(false, ResultType.PARAMETER_NULL, StatusCode.PARAMETERNULL);
        }
        if (departmentMapper.updateByDepartmentIdSelective(department) == 0) {
            log.info("update fail");
            return new ResponseResult(false, ResultType.UPDATE_ERROR, StatusCode.UPDATEERROR);
        }
        return new ResponseResult(true, ResultType.UPDATE_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    /**
     * queryOne: query department by id
     *
     * @param departmentId
     * @return  success: {true, "query success", 201}
     */
    @Override
    public ResponseResult queryOne(String departmentId) {
        if (StringUtils.isBlank(departmentId)) {
            log.info("departmentId is null,query fail");
            return new ResponseResult(false, ResultType.PARAMETER_NULL, StatusCode.PARAMETERNULL);
        }
        Department department = departmentMapper.selectByDepartmentId(departmentId);
        if (null == department) {
            log.info("query fail");
            return new ResponseResult(false, ResultType.FIND_ERROR, StatusCode.FINDERROR);
        }
        return new ResponseResult(department, true, ResultType.FIND_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    /**
     * queryName: query department name by id
     *
     * @param departmentId
     * @return  {true, "query success", 201}
     */
    @Override
    public ResponseResult queryName(String departmentId) {
        if (StringUtils.isBlank(departmentId)) {
            log.info("departmentId is null ,query fail");
            return new ResponseResult(false, ResultType.PARAMETER_NULL, StatusCode.PARAMETERNULL);
        }
        String departmentName = departmentMapper.selectDepartmentNameByPrimaryKey(departmentId);
        if (StringUtils.isBlank(departmentId)) {
            log.info("query fail");
            return new ResponseResult(false, ResultType.FIND_ERROR, StatusCode.FINDERROR);
        }
        log.info("department name is : " + departmentName);
        return new ResponseResult(departmentName, true, ResultType.FIND_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    /**
     * queryAll: query all department
     *
     * @return  success: {departmentList, true, "query success", 201}
     */
    @Override
    public ResponseResult queryAll() {
        List<Department> departmentList = departmentMapper.selectAllDepartment();
        if (CollectionUtils.isEmpty(departmentList)) {
            log.info("Not find department!");
            return new ResponseResult(false, ResultType.FIND_NULL, StatusCode.FINDNULL);
        }
        return new ResponseResult(departmentList, true, ResultType.FIND_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    /**
     * queryAll: query all department
     *
     * @return  success: {departmentList, true, "query success", 201}
     */
    @Override
    public ResponseResult queryAllDetails() {
        List<Department> departmentList = departmentMapper.selectAllDepartment();
        List<DepartmentVO> departmentVOList = new ArrayList<>(departmentList.size());
        for (Department department : departmentList) {
            DepartmentVO departmentVO = convertToDepartmentVO(department);
            departmentVOList.add(departmentVO);
        }
        if (CollectionUtils.isEmpty(departmentVOList)) {
            log.info("Not find department!");
            return new ResponseResult(false, ResultType.FIND_NULL, StatusCode.FINDNULL);
        }
        return new ResponseResult(departmentVOList, true, ResultType.FIND_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    /**
     * @MethodName: queryDepartmentIdAndName
     * @Description: query a department's id and name.
     * @Params:
     * @Return:
     * @Date: 2018/12/2 20:07
     */
    @Override
    public ResponseResult queryAllDepartmentIdAndName() {
        List<Department> departmentList = departmentMapper.selectAllDepartment();
        Map departmentIdAndNameMaps = new HashMap(departmentList.size());
        if (departmentList.size()==0){
            return ResponseResult.resultError(ResultType.FIND_NULL);
        }
        for (Department department : departmentList) {
            departmentIdAndNameMaps.put(department.getDepartmentName(),department.getDepartmentId());
        }
        return new ResponseResult(departmentIdAndNameMaps,true,ResultType.FIND_SUCCESS,StatusCode.OPERATIONSUCCESS);
    }


    /**
     * convertToDepartmentVO: convert department to departmentVO
     *
     * @param department
     * @return  DepartmentVO
     */
    private DepartmentVO convertToDepartmentVO(Department department){
        DepartmentVO departmentVO = new DepartmentVO();
        departmentVO.setDepartmentId(department.getDepartmentId());
        departmentVO.setDepartmentName(department.getDepartmentName());
        departmentVO.setDepartmentRemarks(department.getDepartmentRemarks());
        departmentVO.setDepartmentCreatorName(accountMapper.selectAccountNameByPrimaryKey(department.getDepartmentCreatorAccountId()));
        departmentVO.setDepartmentCreatedTime(DateTool.dateToString(department.getDepartmentCreatedTime(), DataCommon.YYYY_1MM_1DD_HH_MM_SS));
        departmentVO.setDepartmentUpdatorAccountName(accountMapper.selectAccountNameByPrimaryKey(department.getDepartmentUpdatorAccountId()));
        departmentVO.setDepartmentUpdatedTime(DateTool.dateToString(department.getDepartmentUpdatedTime(), DataCommon.YYYY_1MM_1DD_HH_MM_SS));
        return departmentVO;
    }

}
